//
//  BaseViewController.m
//  one·一个  tyw
//
//  Created by tyw on 15/8/20.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "BaseViewController.h"
#import "DayNightManager.h"
#import "MBProgressHUD.h"

@interface BaseViewController ()

@end

@implementation BaseViewController {
    ShareViewNight* _shareViewNight;        //第一种分享页面，首页和东西
    ShareViewAA* _shareViewAA;              //第二种分享页面，文章和问题
    BOOL _shareViewNightIsShow;
    BOOL _shareViewAAIsShow;
    
    UIButton* grayViewBtn;           //笼罩效果
    
    DayNightManager* manager;
    
    
    MBProgressHUD* _hud;            //菊花
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    manager = [DayNightManager sharedManager];

    [self _createNav];              //设置navBar属性
    [self _createGrayViewBtn];      //创建笼罩效果视图
}

//每次当视图出现时，就查看当前模式
- (void)viewWillAppear:(BOOL)animated {
    NSString* type = [manager getCurrentDayNightType];
    [self changeDayNightShow:type];
    
    
    //每次视图出现时，就查看当前导航控制器的子控制器数量，如果大于1，就隐藏tabbar
    if (self.navigationController.viewControllers.count > 1) {
        self.tabBarController.tabBar.hidden = YES;
    } else {
        self.tabBarController.tabBar.hidden = NO;
    }
}

- (void)changeDayNightShow:(NSString*)type {
    if ([type isEqualToString:kDayNightCurrentTypeIsDay]) {
        self.view.backgroundColor = [UIColor whiteColor];
        self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:(235/255.0) green:(235/255.0) blue:(235/255.0) alpha:1];
        self.tabBarController.tabBar.backgroundColor = [UIColor colorWithRed:(235/255.0) green:(235/255.0) blue:(235/255.0) alpha:1];
    } else {
        self.view.backgroundColor = [UIColor colorWithRed:55.0/255 green:55.0/255 blue:55.0/255 alpha:1];
        self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:(20/255.0) green:(20/255.0) blue:(20/255.0) alpha:1];
        self.tabBarController.tabBar.backgroundColor = [UIColor colorWithRed:(20/255.0) green:(20/255.0) blue:(20/255.0) alpha:1];
    }
    //每次改变模式的时候，让这个view去layout，重新根据模式改变状态
    [_shareViewNight setNeedsLayout];
    [_shareViewAA setNeedsLayout];
}

- (void)_createNav {
    //在BaseVC中设置它的navBar的相关信息，应用中所有的VC都将会拥有同样的功能，不需要可以单独设置
    UIImageView* navTitleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navLogo"]];
    self.navigationItem.titleView = navTitleView;
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
}

- (void)_createGrayViewBtn {
    grayViewBtn = [[UIButton alloc] initWithFrame:self.view.bounds];
    grayViewBtn.backgroundColor = [UIColor grayColor];
    grayViewBtn.alpha = 0.01;
    
    [grayViewBtn addTarget:self action:@selector(grayViewBtnAction) forControlEvents:UIControlEventTouchUpInside];
}

//笼罩效果按钮事件
- (void)grayViewBtnAction {
    [self hiddenShareViewNight];
    [self hiddenShareViewAA];
}

#pragma mark - 我是首页君、东西君
- (void)ImHomeOrThingVC {
    UIBarButtonItem* shareBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shareBtn"] style:UIBarButtonItemStylePlain target:self action:@selector(showShareViewNight)];
    self.navigationItem.rightBarButtonItem = shareBtn;
    
    _shareViewNight = [[ShareViewNight alloc] initWithFrame:CGRectMake(0, kHeight, kWidth, 300)];
    _shareViewNightIsShow = NO;
    
    //这句一定要写在这里，因为首页VC调用了这个方法，把这句写在这个方法中，首页VC才算是响应了这个代理
    //把_shareViewNight的代理设置为自己
    _shareViewNight.delegate = self;
}

//让ShareViewNight显示
- (void)showShareViewNight {
    //如果已经显示，直接跳过
    if (_shareViewNightIsShow) {
        return;
    }
    
    
    [self.view addSubview:grayViewBtn];
    [self.view addSubview:_shareViewNight];
    [UIView animateWithDuration:0.5 animations:^{
        _shareViewNight.transform = CGAffineTransformMakeTranslation(0, -300);
        self.tabBarController.tabBar.alpha = 0.01;
        grayViewBtn.alpha = 0.5;
    }];
    _shareViewNightIsShow = YES;
    
    //每次show出来的时候，去调用layout方法，根据当前模式调整状态
    [_shareViewNight setNeedsLayout];
    
    
}
//让ShareViewNight隐藏
- (void)hiddenShareViewNight {
    //如果已经不显示，直接跳过
    if (!_shareViewNightIsShow) {
        return;
    }
    [UIView animateWithDuration:0.5 animations:^{
        _shareViewNight.transform = CGAffineTransformIdentity;
        self.tabBarController.tabBar.alpha = 1;
        grayViewBtn.alpha = 0.01;
    } completion:^(BOOL finished) {
        if (finished) {
            [_shareViewNight removeFromSuperview];
            [grayViewBtn removeFromSuperview];
        }
    }];
    _shareViewNightIsShow = NO;
}

#pragma mark - ShareViewNightDelegate
//收藏按钮被点击时触发
- (void)likeBtnClicked:(LikeBtnActionType)type {
    //如果收藏
    if (type == LikeBtnIsLiked) {
        NSLog(@"收藏了");
    } else {        //如果取消收藏
        NSLog(@"取消收藏");
    }
}
//保存图片按钮被点击时触发
- (void)saveImgBtnClicked {
    NSLog(@"保存图片");
    
}
//日间夜间模式切换按钮被点击时触发
- (void)dayNightBtnClicked:(DayNightBtnActionType)type {
    if (type == DaynightBtnIsDay) {     //如果 切换到日间模式
        NSLog(@"现在是日间模式");
        [self changeDayNightShow:kDayNightCurrentTypeIsDay];
    } else {                            //如果 切换到夜间模式
        NSLog(@"现在是夜间模式");
        //改变外观
        [self changeDayNightShow:kDayNightCurrentTypeIsNight];
    }
}
//取消按钮被点击触发
- (void)cancelBtnClicked {
    [self hiddenShareViewNight];
    [self hiddenShareViewAA];
}


#pragma mark - 我是文章君、问题君
- (void)ImArticleOrQuestionVC {
    UIBarButtonItem* shareBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shareBtn"] style:UIBarButtonItemStylePlain target:self action:@selector(showShareViewAA)];
    self.navigationItem.rightBarButtonItem = shareBtn;
    
    _shareViewAA = [[ShareViewAA alloc] initWithFrame:CGRectMake(0, kHeight, kWidth, 300)];
    _shareViewAAIsShow = NO;
    
    //这里的这些字符串是不是可以写成枚举类型，更方便，待解决
    //为防止循环引用
    __weak BaseViewController* weakSelf = self;
    //调用shareViewAA的setBlock，实现数据传递，在这里根据具体情况  实现相关事件
    [_shareViewAA setBlock:^(NSString* btnClickedType) {
        if ([btnClickedType isEqualToString:kLikeBlockIsLikedStr]) {            //收藏
            NSLog(@"收藏了");
            NSDictionary* dic = @{@"liked" : [NSNumber numberWithBool:YES]};
            [[NSNotificationCenter defaultCenter] postNotificationName:kArticleQuestionLikeBtnNotification object:nil userInfo:dic];
        } else if ([btnClickedType isEqualToString:kLikeBlockNotLikedStr]) {    //取消收藏
            NSLog(@"取消收藏");
            NSDictionary* dic = @{@"liked" : [NSNumber numberWithBool:NO]};
            [[NSNotificationCenter defaultCenter] postNotificationName:kArticleQuestionLikeBtnNotification object:nil userInfo:dic];
        } else if ([btnClickedType isEqualToString:kDayNightBlockIsNightStr]) {    //切换到夜间模式
            NSLog(@"现在是夜间模式");
            [weakSelf changeDayNightShow:kDayNightCurrentTypeIsNight];
        } else if ([btnClickedType isEqualToString:kDayNightBlockIsDayStr]) {    //切换到日间模式
            NSLog(@"现在是日间模式");
            [weakSelf changeDayNightShow:kDayNightCurrentTypeIsDay];
        } else if ([btnClickedType isEqualToString:kShowSetBlockStr]) {    //显示设置
            NSLog(@"显示设置");
        } else if ([btnClickedType isEqualToString:kCancelBlockStr]) {    //取消
            NSLog(@"取消");
            [weakSelf hiddenShareViewNight];
            [weakSelf hiddenShareViewAA];
        } else {
            NSLog(@"shareView中被点击的按钮未知");
        }
    }];
}

//让ShareViewAA显示
- (void)showShareViewAA {
    //如果已经显示，直接跳过
    if (_shareViewAAIsShow) {
        return;
    }
    [self.view addSubview:grayViewBtn];
    [self.view addSubview:_shareViewAA];
    [UIView animateWithDuration:0.5 animations:^{
        _shareViewAA.transform = CGAffineTransformMakeTranslation(0, -300);
        self.tabBarController.tabBar.alpha = 0.01;
        grayViewBtn.alpha = 0.5;
    }];
    _shareViewAAIsShow = YES;
    
    //每次show出来的时候，去调用layout方法，根据当前模式调整状态
    [_shareViewAA setNeedsLayout];
}
//让ShareViewAA隐藏
- (void)hiddenShareViewAA {
    //如果已经不显示，直接跳过
    if (!_shareViewAAIsShow) {
        return;
    }
    [UIView animateWithDuration:0.5 animations:^{
        _shareViewAA.transform = CGAffineTransformIdentity;
        self.tabBarController.tabBar.alpha = 1;
        grayViewBtn.alpha = 0.01;
    } completion:^(BOOL finished) {
        if (finished) {
            [_shareViewAA removeFromSuperview];
            [grayViewBtn removeFromSuperview];
        }
    }];
    _shareViewAAIsShow = NO;
}



#pragma mark - 菊花
//显示菊花
- (void)showHUD:(NSString*)title {
    if (_hud == nil) {
        _hud = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:_hud];
    }
    
    
    [_hud hide:NO];
    _hud.mode = MBProgressHUDModeIndeterminate;
    _hud.labelText = title;
    [_hud showWhileExecuting:@selector(show) onTarget:self withObject:nil animated:YES];
    
    //灰色的背景盖住其他视图
    _hud.dimBackground = YES;
}

- (void)show {
    sleep(60);
}

//- (void)hideHUD {
//    [_hud hide:YES];
//}

//已完成
- (void)completeHUD:(NSString *)title {
    _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark"]];
    
    //
    _hud.mode = MBProgressHUDModeCustomView;
    _hud.labelText = title;
    
    [_hud hide:YES afterDelay:1];
}

//失败
- (void)failHUD:(NSString *)title {
    _hud.mode = MBProgressHUDModeText;
    _hud.labelText = title;
    
    [_hud hide:YES afterDelay:1];
}


@end
